package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ClassificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("admin/category")
@Slf4j
@RestController
@Api(tags = "分类接口")
public class ClassificationController {
    @Autowired
    private ClassificationService classificationService;
    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult category = classificationService.getPage(categoryPageQueryDTO);
        return Result.success(category);
    }

    @PostMapping
    @ApiOperation("新增分类")
    public Result<Category> addCategory(@RequestBody CategoryDTO categoryDTO){
        classificationService.addCategory(categoryDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除分类")
    public Result deleteCategory(@RequestParam("id") Integer id){
        log.info("删除分类id: {}", id); // 查看日志是否打印
        classificationService.deleteCategory(id);
        return Result.success();
    }

    @PostMapping("status/{status}")
    @ApiOperation("启用禁用分类")
    public Result statusStartOrStop(@PathVariable Integer status, Long id){
        classificationService.statusSOS(status,id);
        return Result.success();
    }

//    @GetMapping("{id}")
//    @ApiOperation("查找分类信息")
//    public Result<Category> getCategoryById(@PathVariable Integer id){
//        Category category = classificationService.getCById(id);
//        return Result.success(category);
//    }

    @PutMapping
    @ApiOperation("修改分类信息")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        classificationService.updateC(categoryDTO);
        return Result.success();
    }

    @GetMapping("list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> getCategory(Integer type){
        List<Category> list = classificationService.getListByType(type);
        return Result.success(list);
    }

}
